﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.COM;
using MINERP.MODEL;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.Common;
namespace MINERP.BLL.StoreManage
{
    public class StoreManageBll
    {
        WorkFlowListBLL _wflb;
        public StoreManageBll()
        {
            _wflb = new WorkFlowListBLL();
        }
        public List<ProductStoreInfo> GetAllProductStore()
        {
            List<ProductStoreInfo> rtnList = new List<ProductStoreInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Tb_Product> listP = db.Tb_Product.Where(p=>p.Status==1).ToList();
                foreach (Tb_Product tp in listP)
                {
                   
                    ProductStoreInfo psi=new ProductStoreInfo();
                    psi.ProuctId=tp.ProuctId;
                    psi.ProductName = tp.ProductName + "(" + tp .Width+"x"+tp.Height+ ")";
                    psi.Num=tp.StoreNum;
                    psi.SafeNum=tp.SafeStoreNum;
                    psi.LockNum=tp.LockNum;
                    psi.mnemonicCode = tp.mnemonicCode;
                    tp.Tb_PorductCategoryReference.Load();
                    psi.ClassName = tp.Tb_PorductCategory.ProductCategoryName;
                    rtnList.Add(psi);
                }
                    
            }
            return rtnList;
        }
        public int ChangeStore(ProductStoreInfo inspi, int useId, string inOper,int changeNum)
        {
            int rtn = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Tb_Product tp = db.Tb_Product.FirstOrDefault(p => p.ProuctId == inspi.ProuctId);
                    StoreOperRec sor = new StoreOperRec();
                    sor.OperBeforeLockNum = tp.LockNum;
                    sor.OperBeforeNum = tp.StoreNum;
                    sor.OperRemark = inOper;
                    sor.OperTime = DateTime.Now;
                    sor.users = db.users.FirstOrDefault(u => u.userID == useId);
                    sor.Tb_Product = tp;
                    tp.StoreNum += changeNum;
                    sor.OperAfterLockNum = tp.LockNum;
                    sor.OperAfterNum = tp.StoreNum;
                    db.AddToStoreOperRec(sor);
                    db.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                rtn = 1;
            }
            return rtn;
        }

        public int PandianStore(ProductStoreInfo inspi, int useId, string inOper, int changeNum)
        {
            int rtn = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Tb_Product tp = db.Tb_Product.FirstOrDefault(p => p.ProuctId == inspi.ProuctId);
                    StoreOperRec sor = new StoreOperRec();
                    sor.OperBeforeLockNum = tp.LockNum;
                    sor.OperBeforeNum = tp.StoreNum;
                    sor.OperRemark = inOper;
                    sor.OperTime = DateTime.Now;
                    sor.users = db.users.FirstOrDefault(u => u.userID == useId);
                    sor.Tb_Product = tp;
                    tp.StoreNum = changeNum;
                    sor.OperAfterLockNum = tp.LockNum;
                    sor.OperAfterNum = tp.StoreNum;
                    db.AddToStoreOperRec(sor);
                    db.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                rtn = 1;
            }
            return rtn;
        }
        public int ChangeComplexStore(ComplexOrderInfo inCom, int useId, string inOper, int changeNum,int Techid)
        {
            int rtn = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    foreach (ProductStoreInfo inspi in inCom.SubProductList)
                    {
                        orderInfo order = db.orderInfo.FirstOrDefault(o=>o.OrderProductId==inCom.OrderProductId);
                        bool isSucess=_wflb.OverCurTech(Techid, useId, order, db);
                        if (isSucess)
                        {
                            // Tb_Product tp = db.Tb_Product.FirstOrDefault(p => p.ProuctId == inspi.ProuctId);
                            order.Tb_ProductReference.Load();
                            Tb_Product tp = order.Tb_Product;
                            StoreOperRec sor = new StoreOperRec();
                            sor.OperBeforeLockNum = tp.LockNum;
                            sor.OperBeforeNum = tp.StoreNum;
                            sor.OperRemark = inOper;
                            sor.OperTime = DateTime.Now;
                            sor.users = db.users.FirstOrDefault(u => u.userID == useId);
                            sor.Tb_Product = tp;
                            tp.StoreNum += changeNum;
                            sor.OperAfterLockNum = tp.LockNum;
                            sor.OperAfterNum = tp.StoreNum;
                            db.AddToStoreOperRec(sor);
                           
                        }
                        else
                        {
                            rtn = 1;
                            break;
                            
                        }
                        
                    }
                    if (rtn == 0)
                    {
                        db.SaveChanges();
                    }

                }
            }
            catch (Exception ex)
            {
                rtn = 1;
            }
            return rtn;
        }
        
    }
    
}
